package cn.nawang.ebeim.api.repository.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;

import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.repository.QueryBaseRepository;
import cn.nawang.ebeim.api.utils.Constant;

/**
 * @ClassName: TaskLogQueryRepository
 * @Description: TODO
 * @author fanyb
 * @date Aug 11, 2015 11:56:37 AM
 * @version v1.0
 */
public class TaskLogRepositoryImpl extends BaseQuery {

  @Autowired
  private EntityManagerFactory entityManagerFactory;

  @Autowired
  private QueryBaseRepository queryBaseRepository;

  /**
   * 
   * @author fanyb
   * @Description: 获取该终端任务正在执行的次数
   * @param startTime 起始时间
   * @param endTime 结束时间
   * @param terminalId 终端编号
   * @return int 该终端任务正在执行的次数
   * @date Aug 11, 201511:58:42 AM
   *
   * @修改人
   * @修改日期
   */
  public int getExecutionTaskNum(long startTime, long endTime, String terminalId) {
    String sql = "select count(status) from tb_task_log where terminal_id='" + terminalId + "'";
    if (startTime != 0 && endTime != 0) {
      sql += " and sg_time<=" + endTime + " and sg_time>=" + startTime;
    }
    sql += " and status='"+Constant.TASK_LOG_STATUS_ZERO+"'";
    return queryBaseRepository.countAllData(sql);
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取执行数据源数
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @param terminalId 终端编号
   * @return int 执行数据源数
   * @date Aug 11, 20153:10:22 PM
   *
   * @修改人
   * @修改日期
   */
  public int getDataSouceNum(long startTime, long endTime, String terminalId) {
    String sql =
        "select count(distinct ds_id) from tb_task_log where terminal_id='" + terminalId + "'";
    if (startTime != 0 && endTime != 0) {
      sql += " and sg_time<=" + endTime + " and sg_time>=" + startTime;
    }
    sql += " and status='"+Constant.TASK_LOG_STATUS_TWO+"'";
    return queryBaseRepository.countAllData(sql);
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取该终端的数据源列表
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @param terminalId 终端编号
   * @return List 该终端的数据源列表
   * @date Aug 11, 20154:30:51 PM
   *
   * @修改人
   * @修改日期
   */
  public List getDataSourceList(long startTime, long endTime, String terminalId) {
    String sql =
        "select distinct tl.ds_id from tb_task_log tl,tb_datasource ds where tl.terminal_id='"
            + terminalId + "' and tl.ds_id=ds.id";
    if (startTime != 0 && endTime != 0) {
      sql += " and tl.sg_time<=" + endTime + " and tl.sg_time>=" + startTime;
    }
    sql += " and tl.status='"+Constant.TASK_LOG_STATUS_TWO+"'";
    List objList = null;
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
      Query query = em.createNativeQuery(sql);
      objList = query.getResultList();
    } catch (Exception ex) {
      ex.printStackTrace();
    }finally{
      em.close();
    }
    return objList;
  }
}
